package hehe.validation.base;


import hehe.validation.Validation;
import hehe.validation.util.ValidatorUtil;
import hehe.validation.validator.Validator;

import java.util.HashMap;
import java.util.Map;

/**
 * 验证信息
 *<B>说明：</B>
 *<pre>
 * 略 类详细描述
 *</pre>
 *<B>示例：</B>
 *<pre>
 *  略
 *</pre>
 *<B>日志：</B>
 *<pre>
 *  略
 *</pre>
 *<B>注意事项：</B>
 *<pre>
 *  略
 *</pre>
 */
public class Verify
{
    /**
     * 验证管理对象
     *<B>说明：</B>
     *<pre>
     *  略
     *</pre>
     */
    protected Validation validation;

    protected String validator;

    protected Map<String,Object> attrs = new HashMap<>();

    public Verify(){}

    public Verify(String validator)
    {
        this.validator = validator;
    }

    public Verify(String validator, Map<String,Object> attrs)
    {
        this.validator = validator;
        this.attrs = attrs;
    }

    /**
     * 设置属性
     *<B>说明：</B>
     *<pre>
     * 用户
     *</pre>
     */
    public Verify put(String name, Object value)
    {
        this.attrs.put(name,value);
        return this;
    }

    public Verify putAll(Map<String,Object> attrs)
    {
        this.attrs.putAll(attrs);
        return this;
    }

    /**
     * 设置验证管理类
     *<B>说明：</B>
     *<pre>
     *  略
     *</pre>
     */
    public void setValidation(Validation validation)
    {
        this.validation = validation;
    }

    public String getValidator()
    {
        return this.validator;
    }

    /**
     * 设置提示消息
     *<B>说明：</B>
     *<pre>
     * 略
     *</pre>
     * @param msg 提示消息
     */
    public Verify tip(String msg)
    {
        this.attrs.put(Rule.MESSAGE_ATTR,msg);
        return this;
    }

    /**
     * 设置验证结果非
     *<B>说明：</B>
     *<pre>
     * 略
     *</pre>
     */
    public Verify non()
    {
        this.attrs.put(Rule.AND_OPERATOR,true);
        return this;
    }

    /**
     * 当验证值为空时是否调用验证
     *<B>说明：</B>
     *<pre>
     * 略
     *</pre>
     */
    public Verify onEmpty()
    {
        this.attrs.put(Rule.ONEMPTY_ATTR,true);
        return this;
    }

    /**
     * 创建Validator对象
     *<B>说明：</B>
     *<pre>
     *　略
     *</pre>
     */
    public Validator createValidator()
    {
        try {
            Class validatorClazz = this.validation.buildValidator(this.validator);
            Validator validator = (Validator)validatorClazz.newInstance();
            ValidatorUtil.setAttrs(validator,this.attrs);

            return validator;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

}
